#include <string.h>

#include "./SeqString.h"

// 初始化串
Status initStr(SeqString *s) {
    memset(s->data, '\0', MAXSIZE + 1);
    s->length = 0;
    return OK;
}

// 串的复制
Status StrAssign(SeqString *s1, char* str) {
    int idx = 0;
    while(*(str + idx)) {
        // 第0的位置不放置元素, 下标从1开始算
        s1->data[idx + 1] = *(str + idx);
        idx++;
    }
    // 长度变化
    s1->length = idx;
    return OK;
}

// 串连接
Status StrCat(SeqString *s1, SeqString s2, SeqString s3) {
    if (s2.length + s3.length <= MAXSIZE) {
        // 1. 拼接之后小于最大长度
        int curIdx = 1;
        for (;curIdx <= s2.length; curIdx++) {
            s1->data[curIdx] = s2.data[curIdx];
        }
        for (curIdx = 1; curIdx <= s3.length; curIdx++) {
            s1->data[s2.length + curIdx] = s3.data[curIdx];
        }
        s1->length = s2.length + s3.length;
        
        return TRUE;
    } else if (s2.length < MAXSIZE && s2.length + s3.length > MAXSIZE) {
        // 2. 拼接之后大于最大长度, 拼接的第一个串小于最大长度, 再加上第二个串时大于了最大长度
        int curIdx = 1;
        for (;curIdx <= s2.length; curIdx++) {
            s1->data[curIdx] = s2.data[curIdx];
        }
        for (curIdx = 1; curIdx <= (MAXSIZE - s2.length); curIdx++) {
            s1->data[s2.length + curIdx] = s3.data[curIdx];
        }
        s1->length = MAXSIZE;
    } else {
        // 3. 拼接的第一个串大于最大长度
        int curIdx = 1;
        for (;curIdx <= MAXSIZE; curIdx++) {
            s1->data[curIdx] = s2.data[curIdx];
        }
        s1->length = MAXSIZE;
    }
    return FALSE;
}

// 求串长
unsigned int StrLen(SeqString s){
    return s.length;
}

// 串判空
Status isEmpty(SeqString s) {
    return s.length == 0;
}

// 清空串
Status ClearString(SeqString *s) {
    memset(s->data, '\0', MAXSIZE + 1);
    s->length = 0;
    return OK;
}

// 串比较
Status StrCompare(SeqString s1, SeqString s2) {
    // 一个字符一个字符的比较 ASCLL 码值
    unsigned int idx = 0;
    while(idx <= s1.length && idx <= s2.length) {
        if (s1.data[idx] > s2.data[idx]) {
            // 第一个大
            return 1;
        } else if(s1.data[idx] < s2.data[idx]) {
            // 第二个大
            return 2;
        }
        ++idx;
    }
    // 说明s1或者s2字符串中有一个到头了, 然后比较串的长度
    if (s1.length > s2.length) {
        return 1;
    } else if(s1.length < s2.length) {
        return 2;
    }
    // 相等
    return 0;
}

// 求子串(包含前面, 不包含后面)
Status SubString(SeqString s1, unsigned int startIndex, unsigned int endIndex, SeqString *result) {
    if (startIndex < 1 || startIndex > s1.length || endIndex < 1 || endIndex > s1.length) {
        return ERROR;
    }

    if (startIndex >= endIndex) {
        return ERROR;
    }

    int idx = 0;
    while(startIndex < endIndex) {
        result->data[idx + 1] = s1.data[startIndex + idx];
        idx++;
    }
    return OK;
}

// 子串插入
Status strInsert(SeqString *s, int pos, SeqString s2) {
    if (pos < 1 || pos > s->length || (s->length + s2.length) > MAXSIZE) {
        // 位置不合法, 或者长度已满
        return ERROR;
    }

    // 从后往前, 先移动主串
    int currentPos = s->length;
    while(currentPos >= pos) {
        s->data[currentPos + s2.length] = s->data[currentPos];
        --currentPos;
    }

    currentPos = 1;
    while(currentPos <= s2.length) {
        s->data[pos + currentPos - 1] = s2.data[currentPos];
        ++currentPos;
    }
    return OK;
}

// BF算法, 暴力破解法
// 注意SeqString 的下标是从1开始的
int Index_BF(SeqString s, SeqString t) {
    int i = 0, j = 1;
    while(i <= s.length && j <= t.length) {
        if (s.data[i] == t.data[j]) {
            i++;
            j++;
        } else {
            i = i - j + 2;
            j = 1;
        }
    }
    if (j >= t.length) {
        return i - t.length;
    } else {
        // 0表示模式串匹配不成功
        return 0;
    }
}

int main() {
    SeqString s;
    initStr(&s);
    StrAssign(&s, "abcdef");

    SeqString s1, s2, s3;
    initStr(&s1);
    initStr(&s2);
    initStr(&s3);
    StrAssign(&s2, "abcdef");
    StrAssign(&s3, "ghijk");
    StrCat(&s1, s2, s3);
    strInsert(&s1, 4, s3);
    return 0;
}